package itsm.isperp.module.controller.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.app.AppUploadFile;
import itsm.isperp.module.service.app.AppUploadFileService;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 数据字典控制器
 * 
 * @author lizx
 * @date 2013-2-16
 * @version V1.0
 */
@Controller
@RequestMapping("app/uploadFile")
public class AppUploadFileController extends BaseController<AppUploadFile> {

	/**
	 * 数据字典管理服务
	 */
	@Autowired
	protected AppUploadFileService appFileUploadService;

	@Override
	public AppUploadFileService getService() {
		return appFileUploadService;
	}

	@Override
	public String getPageTitle() {
		return "系统字典";
	}

	@ResponseBody
	@RequestMapping("getBySourceId")
	public DataResponse<AppUploadFile> getBySourceId(String sourceId) {
		return this.getService().getBySourceId(sourceId);

	}

	@RequestMapping("getFileInfo/{id}")
	@ResponseBody
	public AppUploadFile getFileInfo(@PathVariable("id") String id) {

		return this.getService().findOne(id);
	}

	@RequestMapping("deleteFile/{id}")
	@ResponseBody
	public ResultMessage deleteUploadFile(@PathVariable("id") String id) {

		AppUploadFile file = this.getService().findOne(id);
		String path = ContextHolder.getApplicationPath() + file.getPath();
		new File(path).deleteOnExit();
		this.getService().delete(id);

		return ResultMessage.newInstance(true);
	}

}
